﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using My_Patient.DataModel;
using My_Patient.Model;
using DevExpress.XtraGrid.Columns;

namespace My_Patient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            this.gridView1.Columns.Clear();
            this.gridControl1.DataSource = null;
            using (var context = new PatientEntities())
            {
                IEnumerable<PATIENT> pats = context.PATIENT.Where(p => true);
                List<PatientModel> patList = new List<PatientModel>();
                foreach (var pat in pats)
                {
                    var patModel = new PatientModel(pat);
                    patList.Add(patModel);
                }

                this.gridControl1.DataSource = patList;
            }
            GridColumn delLinkHiper = new GridColumn();
            delLinkHiper.Caption = "删除";
            delLinkHiper.Visible = true;

            delLinkHiper.ColumnEdit = reItemDelete;
            this.gridView1.Columns.Add(delLinkHiper);
            delLinkHiper.VisibleIndex = 7;

            this.gridView1.BestFitColumns();
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            var dr = this.gridView1.GetRow(this.gridView1.FocusedRowHandle) as PatientModel;
            using (var context = new PatientEntities())
            {
                var pat = context.PATIENT.Where(p => p.PAT_ID == dr.id).FirstOrDefault();
                pat.PAT_AGE = dr.pAge;
                pat.PAT_DOCTOR = dr.pDoctor;
                pat.PAT_NAME = dr.pName;
                pat.PAT_NOSTS = dr.pNosts;
                pat.PAT_SEX = dr.pSex == "男" ? true : false;
                context.SaveChanges();
            }
            MessageBox.Show("修改成功！");
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (String.IsNullOrWhiteSpace(this.TB_patName.Text))
            {
                MessageBox.Show("必须填写病人姓名！");
                return;
            }
            using (var context = new PatientEntities())
            {
                var patEx = context.PATIENT.OrderByDescending(p => p.PAT_ID).FirstOrDefault();
                PATIENT pat = new PATIENT();
                int age;
                int no = int.Parse(patEx.PAT_NO);
                string nostr = "00000" + (no + 1).ToString();
                pat.PAT_ID = patEx.PAT_ID + 1;
                pat.PAT_AGE = int.TryParse(this.TB_patAge.Text, out age) ? age : 0;
                pat.PAT_DOCTOR = this.TB_patDoc.Text;
                pat.PAT_NAME = this.TB_patName.Text;
                pat.PAT_NO = nostr.Substring(nostr.Length - 5, 5);
                pat.PAT_SEX = this.CB_patSex.SelectedText == "男" ? true : false;
                pat.PAT_NOSTS = this.TB_patNotes.Text;
                context.PATIENT.Add(pat);
                context.SaveChanges();
            }
            MessageBox.Show("新增成功！");
            LoadData();
        }

        private void reItemDelete_Click(object sender, EventArgs e)
        {
            var dr = this.gridView1.GetRow(this.gridView1.FocusedRowHandle) as PatientModel;
            using (var context = new PatientEntities())
            {
                var pat = context.PATIENT.Where(p => p.PAT_ID == dr.id).FirstOrDefault();
                context.PATIENT.Remove(pat);
                context.SaveChanges();
            }
            MessageBox.Show("删除成功！");
            LoadData();
        }
    }
}
